package com.lzx.applib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

import com.lzx.applib.R;
import com.lzx.applib.utils.DpSpDip2Px;

/**
 * Created by Lzx on 16/11/14 0014.
 */
public class SelectableText extends TextView {

    public static final String TAG = "SelectableText";

    int selectedBg;
    int unselectedBg;
    int textSelectedColor;
    int textUnselectedColor;

    public SelectableText(Context context) {
        this(context, null);
    }

    public SelectableText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setGravity(Gravity.CENTER);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SelectableText);
        selectedBg = array.getResourceId(R.styleable.SelectableText_selectedBg, 0);
        unselectedBg = array.getResourceId(R.styleable.SelectableText_unselectedBg, 0);
        textSelectedColor = array.getColor(R.styleable.SelectableText_textSelectedColor, Color.TRANSPARENT);
        textUnselectedColor = array.getColor(R.styleable.SelectableText_textUnselectedColor, Color.TRANSPARENT);
        boolean selected = array.getBoolean(R.styleable.SelectableText_selected, false);
        array.recycle();

        setSelected(selected);
        setSelected(false);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            setTextColor(textSelectedColor);
            if (selectedBg != 0) setBackgroundResource(selectedBg);
        } else {
            setTextColor(textUnselectedColor);
            if (unselectedBg != 0) setBackgroundResource(unselectedBg);
        }
        postInvalidate();
    }
}
